/**
 * 
 */
package com.base.myproject.client.busi.b2c;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.JDBCModelData;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.tools.SendServlet;
import com.base.myproject.client.tools.XML;
import com.base.myproject.client.ui.BaseGrid;
import com.base.myproject.client.ui.BaseImageList;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.Style.VerticalAlignment;
import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.TableData;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.core.client.GWT;
import com.google.gwt.xml.client.Document;
import com.google.gwt.xml.client.Element;
import com.google.gwt.xml.client.Node;
import com.google.gwt.xml.client.NodeList;
import com.google.gwt.xml.client.XMLParser;

/**
 * @author zgw
 *2009-7-14 下午07:49:05
 * javazgw@gmail.com
 */
public class B2cGcode extends BaseJDBCPanel{
	static BaseStore b2cgcode  = null;
	static BaseGrid b2cgcodegrid  = null;
	public B2cGcode() {
		super();
		init();
	}

	public void init() {

		
		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);  
	    centerData.setMargins(new Margins(5, 5, 0, 0));  
	this.add(initWeiget(),centerData);
	}
	public ContentPanel initWeiget() {
		ContentPanel panel = new ContentPanel();
//		TabPanel tabpanel = new TabPanel();
//
//		tabpanel.setAutoWidth(true);
//	
//		tabpanel.setAutoHeight(true);
//		
//		TabItem normal1 = new TabItem("商品明细");
	
		VerticalPanel layoutcontainer = new VerticalPanel();
		layoutcontainer.add(CreateB2cGcode());
		layoutcontainer.add(createImagePanel());
		
//		normal1.add(layoutcontainer);
//	
//		tabpanel.add(normal1);
		TableLayout tl = new TableLayout(2);
		TableData td =new TableData();
		td.setColspan(1);
		td.setRowspan(1);
		td.setVerticalAlign(VerticalAlignment.TOP);
		td.setHorizontalAlign(HorizontalAlignment.LEFT);
		
		
		panel.setLayout(tl);

		panel.add(layoutcontainer,td);
		panel.add(CreateGcodeAttrg(),td);
		
		 setMaincomponent(b2cgcodegrid);
			registerMainStore(b2cgcode);
			this.registerStore(b2cgcode);
		return panel;
	}

	
	
	private ContentPanel CreateB2cGcode()
	{
		ContentPanel panel = new ContentPanel();
	
		panel.setAutoWidth(true);
		CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());
		ColumnConfig gcode = new ColumnConfig("gcode", "商品大类编码", 100);
		TextField<String> gcodetext = new TextField<String>();
		gcode.setEditor(new CellEditor(gcodetext));
		columns.add(gcode);
		ColumnConfig gname = new ColumnConfig("gname", "商品大类名称", 100);
		TextField<String> gnametext = new TextField<String>();
		gname.setEditor(new CellEditor(gnametext));
		columns.add(gname);
		
		ColumnConfig treecode = new ColumnConfig("treecode", "商品分类树编码", 100);

	/*	ModelType combotype = new ModelType();
		combotype.setRoot("records");
		combotype.setRecordName("record");
		combotype.setTotalName("totle");
		combotype.addField("code", "ccode");
		combotype.addField("name", "cname");
		StoreInfo comboinfo = new StoreInfo();

		comboinfo.setTablename("ccode");
		comboinfo.setSql("select * from ccode");
		BaseComboBox combo = new BaseComboBox(combotype, comboinfo);
//		combo.setDisplayField("name");
//		combo.setValueField("value");
		BaseCellEditorCB editor = new BaseCellEditorCB(combo) ;
			   
		treecode.setEditor(editor);
		
		treecode.setRenderer(editor);*/
		TextField<String> treecodetext = new TextField<String>();
		treecode.setEditor(new CellEditor(treecodetext));
		columns.add(treecode);
		
		
		
		ColumnConfig b2cgcodecolumn = new ColumnConfig("b2cgcode", "商品编码", 100);
		TextField<String> b2cgcodetext = new TextField<String>();
		b2cgcodecolumn.setEditor(new CellEditor(b2cgcodetext));
		columns.add(b2cgcodecolumn);
		ColumnConfig b2cgname = new ColumnConfig("b2cgname", "商品名称", 100);
		TextField<String> b2cgnametext = new TextField<String>();
		b2cgname.setEditor(new CellEditor(b2cgnametext));
		columns.add(b2cgname);

		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");

		type.addField("gcode");
		type.addField("gname");
		type.addField("b2cgcode");
		type.addField("b2cgname");
		type.addField("mainimagepath");
		type.addField("treecode");
		type.addField("detailimage");
		

		StoreInfo info = new StoreInfo();

		info.setTablename("b2cgcode");
		info.setKeyname(new String[]{"b2cgcode"});
		info.setSql("select * from b2cgcode");
//		info.setParent(store);// 关联主表
//		info.setAutoinccolumn("idx"); // 自增字段，如果子表一定需要
//		info.setLinkparentcolumns(new String[] { "ccode" }); // 关联主表字段
		 b2cgcode = new BaseStore<JDBCModelData>(type, info);
//		gudong.setName("gudong");
//		gudong.setKeyname(new String[] { "ccode", "idx" }); // 主键
		ColumnModel cm = new ColumnModel(columns);
		 b2cgcodegrid = new BaseGrid(b2cgcode, cm);

		cbs.setSelectionMode(SelectionMode.SINGLE);

		b2cgcodegrid.setSelectionModel(cbs);
		b2cgcodegrid.setAutoHeight(true);
		b2cgcodegrid.setAutoWidth(true);

		registerStore(b2cgcode);
		panel.add(b2cgcodegrid);
		return panel;
	}
	
	 public ContentPanel createImagePanel() {  
		 ContentPanel panel = new ContentPanel();  
		
		  ModelType type = new ModelType();  
		  type.setRoot("records") ;  
		    type.setRecordName("record");  

		    type.addField("b2cgcode");  
		    type.addField("imagepath");  
		    type.addField("idx");  
		    
		    StoreInfo info= new StoreInfo();
		    info.setTablename("b2cgcodeimage");
		    info.setSql("select * from b2cgcodeimage");
		    
		   final BaseStore<BaseModelData> imagestore = new BaseStore<BaseModelData>(type,info);
		
		    imagestore.setParent(b2cgcode);
		    info.setKeyname(new String[]{"b2cgcode","idx"});
		    imagestore.setLinkparentcolumns(new String[]{"b2cgcode"});
		    imagestore.setParent(b2cgcode);
		    BaseImageList bil = new BaseImageList(imagestore,b2cgcode);
		  // bil.setParam("table=b2cgcodeimage&key=gcode&keyvalue="+b2cgcode.getValue("gcode")+"&idx=idx&bcode="+Env.getEnvbcode());
		    panel.setHeaderVisible(false);
		    registerStore(imagestore);
		   panel.add(bil);
		 return panel;
	 }
	 /**
	  * 商品属性
	  * @return
	  */
	 private ContentPanel CreateGcodeAttrg()
		{
			ContentPanel panel = new ContentPanel();
			CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
			List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
			columns.add(cbs.getColumn());
			
			ColumnConfig b2cgcodec = new ColumnConfig("b2cgcode", "b2cgcode", 100);
			columns.add(b2cgcodec);
			ColumnConfig attrcode = new ColumnConfig("attrcode", "商品属性编码", 100);
//			TextField<String> attrcodetext = new TextField<String>();
//			attrcode.setEditor(new CellEditor(attrcodetext));
			columns.add(attrcode);
			
			ColumnConfig attrname = new ColumnConfig("attrname", "商品属性名称", 100);
			TextField<String> attrnametext = new TextField<String>();
			attrname.setEditor(new CellEditor(attrnametext));
			columns.add(attrname);
			ColumnConfig attrvalue = new ColumnConfig("attrvalue", "商品属性值", 100);
			TextField<String> attrvaluetext = new TextField<String>();
			attrvalue.setEditor(new CellEditor(attrvaluetext));
			columns.add(attrvalue);
			
	

			ModelType type = new ModelType();
			type.setRoot("records");
			type.setRecordName("record");

			type.addField("gcode");
			type.addField("b2cgcode");
			type.addField("attrcode");
			type.addField("attrname");
			type.addField("idx");
			type.addField("attrvalue");
			
		


			StoreInfo info = new StoreInfo();

			info.setTablename("b2cgcodeattrg");
			
			info.setSql("select * from b2cgcodeattrg");
			info.setParent(b2cgcode);// 关联主表
			info.setAutoinccolumn("idx"); // 自增字段，如果子表一定需要
			info.setLinkparentcolumns(new String[] { "b2cgcode" }); // 关联主表字段
			info.setKeyname(new String[] { "b2cgcode", "idx" });
			final BaseStore<JDBCModelData> gcodeattrg = new BaseStore<JDBCModelData>(type, info);
//			gudong.setName("gudong");
//			gudong.setKeyname(new String[] { "ccode", "idx" }); // 主键
			ColumnModel cm = new ColumnModel(columns);
			BaseGrid attrgrid = new BaseGrid(gcodeattrg, cm);

			cbs.setSelectionMode(SelectionMode.MULTI);

			attrgrid.setSelectionModel(cbs);
			attrgrid.setAutoHeight(true);
			//attrgrid.setAutoWidth(true);

			ToolBar tb = new ToolBar();
			Button addattr = new Button("添加该商品属性");
			addattr.addSelectionListener(new SelectionListener<ButtonEvent>(){

				@Override
				public void componentSelected(ButtonEvent ce) {
					int size = b2cgcodegrid.getSelectionModel().getSelectedItems().size();
					if(size<1)
					{
						MessageBox.alert("警告", "未选择将要添加属性的商品。操作将取消！", null);
						return ;
					}
					
					String gcode = b2cgcode.getValue("gcode").toString();
					System.out.println("gcode="+gcode);
					
					String sql  = "select * from b2cgcodeattrgroup where attrcode %26 cast((select attrcode from gcode where gcode ='"+gcode+"') as int) = attrcode";
					
					SendServlet ss = new SendServlet();
					String datawinstr = ss.getStringx("/"+GWT.getModuleName()+"/BaseServlet", "Action=getdatasql&sql="+sql+"");
					  Document doc = XMLParser.parse((String) datawinstr);
		
					    NodeList list = doc.getElementsByTagName("record");
						 
					    for (int i = 0; i < list.getLength(); i++) {
					    	NodeList recordlist =list.item(i).getChildNodes();
					    	
						      Node node = list.item(i);
						      Element elem = (Element) node;
						      
						      System.out.println( XML.getValue(elem,"attrcode"));
						      System.out.println( XML.getValue(elem,"attrname"));
						      
						      String attrcode = XML.getValue(elem,"attrcode");
						      String attrname = XML.getValue(elem,"attrname");
						    	 int count = gcodeattrg.getCount();
						    	 boolean b = false;
						    	 for(int k=0;k<count;k++)
						    	 {
						    		String attrcodetemp= gcodeattrg.getValue(k, "attrcode").toString();
						    		if(attrcode.equals(attrcodetemp))
						    		{
						    			b = true;
						    			break;
						    		}
						    	 }
						    	 if(!b)
						    	 {
						    		 HashMap hm = new HashMap();
						    	    	
						    		 hm.put("attrcode", attrcode);
						    		 hm.put("attrname", attrname);
						    			JDBCModelData modeldata = new JDBCModelData(hm);
						    		 gcodeattrg.add(modeldata) ;
						    	 }
						   //   for(int j=0; j<recordlist.getLength();j++)
							     {
//						    	  Node recordnode = recordlist.item(j);
//						    	  Element recordelem = (Element) recordnode;
//						    	  String name = recordelem.getChildNodes().item(j).getNodeName();
//						    	  System.out.println("name="+name);
//						    	  String value = XML.getValue(elem,name);
//						    	  System.out.println("value="+value);
//						    	  for(int k=0;k<)
//						    	  {
//						    		  
//						    	  }
							    	 
//							    	 String name =  elem.getChildNodes().item(j).getNodeName();
//							    	 String value = XML.getValue(elem,name);
//							    	 System.out.println("---"+XML.getValue(elem,"attrcode"));
//							    	 System.out.println("name= "+name+"value= "+value);
//							    	 int count = gcodeattrg.getCount();
//							    	 for(int k=0;k<count;k++)
//							    	 {
//							    		 
//							    	 }
							     }
						      
		
					    }
				}
				
			});
			
			addattr.setToolTip("不会删除原有的属性");
			tb.add(addattr);
			panel.setTopComponent(tb);
			registerStore(gcodeattrg);
			attrgrid.setAutoHeight(true);
			panel.add(attrgrid);
		
			panel.setScrollMode(Scroll.AUTO);
			return panel;
			
		}
}
